package com.rowline.admin.module.util.utils;

import org.apache.commons.lang3.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

/**
 * ClassName:HttpRequestUtil
 * Created by 245167
 * on 2017/8/10  10:43
 * description:
 */
public class HttpRequestUtil {


    public static Boolean isAjax(HttpServletRequest request) {

        if (StringUtils.contains(request.getHeader("Content-Type"), "application/json")
                || StringUtils.contains(request.getHeader("Content-Type"), "application/x-www-form-urlencoded")
                || StringUtils.contains(request.getHeader("X-Requested-With"), "XMLHttpRequest")	) {
            return true;
        }
        return false;
    }

    /**
     * 获取请求heads
     * @param request
     * @return
     */
    public static Map<String, String> getHeadersInfo(HttpServletRequest request) {
        Map<String, String> map = new HashMap<String, String>();
        @SuppressWarnings("rawtypes")
        Enumeration headerNames = request.getHeaderNames();
        while (headerNames.hasMoreElements()) {
            String key = (String) headerNames.nextElement();
            String value = request.getHeader(key);
            map.put(key, value);
        }
        return map;
    }


    /**
     * 获取请求params
     * @param request
     * @return
     */
    public static Map<String,String> getParamers(HttpServletRequest request){
        Map<String,String> parameters = new HashMap<String,String>();
        @SuppressWarnings("rawtypes")
        Enumeration parmNames = request.getParameterNames();
        while (parmNames.hasMoreElements()) {
            String key = (String) parmNames.nextElement();
            String value = request.getParameter(key);
            parameters.put(key, value);
        }
        return parameters;
    }

    /**
     * 获取客户端的IP地址的方法是：request.getRemoteAddr() ，这种方法在大部分情况下都是有效的
     * 经过代理以后，由于在客户端和服务之间增加了中间层，因此服务器无法直接拿到客户端的IP，服务器
     * 端应用也无法直接通过转发请求的地址返回给客户端。但是在转发请求的HTTP头信息中，增加了
     * X－FORWARDED－FOR信息。用以跟踪原有的客户端IP地址和原来客户端请求的服务器地址。
     * @param request
     * @return
     */
    public static String getIpAddress(HttpServletRequest request) {

        String ip = request.getHeader("x-forwarded-for");
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_CLIENT_IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_X_FORWARDED_FOR");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        return ip;
    }
}
